package com.dragon.modules.yuegou.client.ali.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.FileItem;
import com.alipay.api.domain.AlipayMarketingCardActivateurlApplyModel;
import com.alipay.api.domain.AlipayMarketingCardTemplateCreateModel;
import com.alipay.api.domain.AlipayMarketingCardTemplateModifyModel;
import com.alipay.api.domain.AlipayMarketingCardTemplateQueryModel;
import com.alipay.api.domain.AlipayOpenAppMessageTopicSubscribeModel;
import com.alipay.api.request.AlipayMarketingCardActivateurlApplyRequest;
import com.alipay.api.request.AlipayMarketingCardFormtemplateSetRequest;
import com.alipay.api.request.AlipayMarketingCardTemplateCreateRequest;
import com.alipay.api.request.AlipayMarketingCardTemplateModifyRequest;
import com.alipay.api.request.AlipayMarketingCardTemplateQueryRequest;
import com.alipay.api.request.AlipayOfflineMaterialImageUploadRequest;
import com.alipay.api.request.AlipayOpenAppMessageTopicSubscribeRequest;
import com.alipay.api.response.AlipayMarketingCardActivateurlApplyResponse;
import com.alipay.api.response.AlipayMarketingCardFormtemplateSetResponse;
import com.alipay.api.response.AlipayMarketingCardTemplateCreateResponse;
import com.alipay.api.response.AlipayMarketingCardTemplateModifyResponse;
import com.alipay.api.response.AlipayMarketingCardTemplateQueryResponse;
import com.alipay.api.response.AlipayOfflineMaterialImageUploadResponse;
import com.alipay.api.response.AlipayOpenAppMessageTopicSubscribeResponse;
import com.dragon.modules.yuegou.client.ali.request.FileUploadRequest;
import com.dragon.modules.yuegou.client.ali.request.MemActivateurlGetRequest;
import com.dragon.modules.yuegou.client.ali.request.MemFormTemplateSetRequest;
import com.dragon.modules.yuegou.client.ali.request.MemTemplateGetRequest;
import com.dragon.modules.yuegou.client.ali.request.MemTemplateSetRequest;
import com.dragon.modules.yuegou.client.ali.request.MessageTopicRequest;
import com.dragon.modules.yuegou.client.ali.response.FileUploadResponse;
import com.dragon.modules.yuegou.client.ali.response.MemTemplateGetResponse;
import com.dragon.modules.yuegou.client.ali.response.TemplateSetResponse;
import com.dragon.modules.yuegou.client.ali.service.MemTemplateService;
import com.dragon.modules.yuegou.properties.AlipayXcxConfig;
import com.dragon.modules.yuegou.properties.IsvAlipayProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service("memTemplateService")
public class MemTemplateServiceImpl implements MemTemplateService {

    @Autowired
    private IsvAlipayProperties isvAlipayProperties;
    
    
	@Override
	public FileUploadResponse upload(FileUploadRequest request) {
		FileUploadResponse response = new FileUploadResponse();
		AlipayClient alipayClient =  new DefaultAlipayClient(isvAlipayProperties.getServerUrl(),isvAlipayProperties.getAppId(),
				isvAlipayProperties.getPrivateKey(),isvAlipayProperties.getFormat(),isvAlipayProperties.getCharset(),isvAlipayProperties.getAlipayPublicKey(),isvAlipayProperties.getSignType());
		AlipayOfflineMaterialImageUploadRequest setRequest = new AlipayOfflineMaterialImageUploadRequest();
		setRequest.setImageName(request.getImageName());
		setRequest.setImageType(request.getImageType());
		setRequest.setImageContent(new FileItem(request.getImageName(),request.getImageContent(),request.getImageType()));
		setRequest.putOtherTextParam("app_auth_token", request.getAppAuthToken());
        try {
        	AlipayOfflineMaterialImageUploadResponse setResponse = alipayClient.execute(setRequest);
            log.debug(JSON.toJSONString(setResponse));
        	BeanUtils.copyProperties(setResponse,response);
            if(setResponse.isSuccess()){
            	log.debug("调用成功");
            	response.setImageUrl(setResponse.getImageUrl());
            	response.setImageId(setResponse.getImageId());
            } else {
            	log.debug("调用失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }  	
		return response;
	}

	
	@Override
	public TemplateSetResponse templateSet(MemTemplateSetRequest request) {
		TemplateSetResponse response = new TemplateSetResponse();
        AlipayClient alipayClient =  new DefaultAlipayClient(isvAlipayProperties.getServerUrl(),isvAlipayProperties.getAppId(),
        		isvAlipayProperties.getPrivateKey(),isvAlipayProperties.getFormat(),isvAlipayProperties.getCharset(),isvAlipayProperties.getAlipayPublicKey(),isvAlipayProperties.getSignType());
        AlipayMarketingCardTemplateCreateRequest setRequest = new AlipayMarketingCardTemplateCreateRequest();
        setRequest.putOtherTextParam(AlipayXcxConfig.APP_AUTH_TOKEN_STRING, request.getAppAuthToken());
        AlipayMarketingCardTemplateCreateModel model = new AlipayMarketingCardTemplateCreateModel();
        model.setRequestId(request.getSetId());
        model.setCardType(request.getCardType());
        model.setBizNoPrefix(request.getBizNoPrefix());
        model.setBizNoSuffixLen(request.getBizNoSuffixLen());
        model.setWriteOffType(request.getWriteOffType());
        model.setTemplateStyleInfo(request.getTemplateStyleInfo());
        model.setColumnInfoList(request.getColumnInfoList());
        model.setFieldRuleList(request.getFieldRuleList());
        setRequest.setBizModel(model);
        try {
        	AlipayMarketingCardTemplateCreateResponse setResponse = alipayClient.execute(setRequest);
            log.debug(JSON.toJSONString(setResponse));
        	BeanUtils.copyProperties(setResponse,response);
            if(setResponse.isSuccess()){
            	response.setTemplateId(setResponse.getTemplateId());
            	log.debug("调用成功");
            } else {
            	log.debug("调用失败");
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }  
        return response;
	}


	@Override
	public TemplateSetResponse templateUpd(MemTemplateSetRequest request) {
		TemplateSetResponse response = new TemplateSetResponse();
        AlipayClient alipayClient =  new DefaultAlipayClient(isvAlipayProperties.getServerUrl(),isvAlipayProperties.getAppId(),
        		isvAlipayProperties.getPrivateKey(),isvAlipayProperties.getFormat(),isvAlipayProperties.getCharset(),isvAlipayProperties.getAlipayPublicKey(),isvAlipayProperties.getSignType());
 		AlipayMarketingCardTemplateModifyRequest  setRequest = new AlipayMarketingCardTemplateModifyRequest();
        setRequest.putOtherTextParam(AlipayXcxConfig.APP_AUTH_TOKEN_STRING, request.getAppAuthToken());
		AlipayMarketingCardTemplateModifyModel model = new AlipayMarketingCardTemplateModifyModel();
		model.setRequestId(request.getSetId());
		model.setTemplateId(request.getTemplateId());
		model.setBizNoPrefix(request.getBizNoPrefix());
		model.setWriteOffType(request.getWriteOffType());
		model.setTemplateStyleInfo(request.getTemplateStyleInfo());
		model.setColumnInfoList(request.getColumnInfoList());
		model.setFieldRuleList(request.getFieldRuleList());
		setRequest.setBizModel(model);
		try {
			AlipayMarketingCardTemplateModifyResponse  setResponse = alipayClient.execute(setRequest);
			log.debug(JSON.toJSONString(setResponse));
			BeanUtils.copyProperties(setResponse,response);
			if (setResponse.isSuccess()) {
				log.debug("调用成功");
			} else {
				log.debug("调用失败");
			}
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
        return response;
	}


	@Override
	public MemTemplateGetResponse templateGet(MemTemplateGetRequest request) {
		MemTemplateGetResponse response = new MemTemplateGetResponse();
        AlipayClient alipayClient =  new DefaultAlipayClient(isvAlipayProperties.getServerUrl(),isvAlipayProperties.getAppId(),
        		isvAlipayProperties.getPrivateKey(),isvAlipayProperties.getFormat(),isvAlipayProperties.getCharset(),isvAlipayProperties.getAlipayPublicKey(),isvAlipayProperties.getSignType());
        AlipayMarketingCardTemplateQueryRequest   setRequest = new AlipayMarketingCardTemplateQueryRequest();
        setRequest.putOtherTextParam(AlipayXcxConfig.APP_AUTH_TOKEN_STRING, request.getAppAuthToken());
		AlipayMarketingCardTemplateQueryModel model = new AlipayMarketingCardTemplateQueryModel();
		model.setTemplateId(request.getTemplateId());
		setRequest.setBizModel(model);
		try {
			AlipayMarketingCardTemplateQueryResponse  queryResponse  = alipayClient.execute(setRequest);
			log.debug(JSON.toJSONString(queryResponse));
			BeanUtils.copyProperties(queryResponse,response);
			if (queryResponse.isSuccess()) {
				log.debug("调用成功");
			} else {
				log.debug("调用失败");
			}
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		return response;
	}


	@Override
	public void formTemplate(MemFormTemplateSetRequest request) {
        AlipayClient alipayClient =  new DefaultAlipayClient(isvAlipayProperties.getServerUrl(),isvAlipayProperties.getAppId(),
        		isvAlipayProperties.getPrivateKey(),isvAlipayProperties.getFormat(),isvAlipayProperties.getCharset(),isvAlipayProperties.getAlipayPublicKey(),isvAlipayProperties.getSignType());
 		AlipayMarketingCardFormtemplateSetRequest setRequest = new AlipayMarketingCardFormtemplateSetRequest();
        setRequest.putOtherTextParam(AlipayXcxConfig.APP_AUTH_TOKEN_STRING, request.getAppAuthToken());
		JSONObject bizParams = new JSONObject();         
		bizParams.put("template_id", request.getTemplateId());  
		JSONObject fields = new JSONObject(); 
		JSONObject requiredField = new JSONObject();     
		requiredField.put("common_fields", request.getRequiredFields());     
		JSONObject optionalField = new JSONObject();  
		optionalField.put("common_fields", request.getOptionalFields());    
		fields.put("required", requiredField);     
		fields.put("optional", optionalField);  
		bizParams.put("fields", fields);       
		setRequest.setBizContent(bizParams.toString());
		try {
			AlipayMarketingCardFormtemplateSetResponse   setResponse  = alipayClient.execute(setRequest);
			log.debug(JSON.toJSONString(setResponse));
			if (setResponse.isSuccess()) {
				log.debug("调用成功");
			} else {
				log.debug("调用失败");
			}
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}			
	}


	@Override
	public String getActivateurl(MemActivateurlGetRequest request) {
		String url = null;
        AlipayClient alipayClient =  new DefaultAlipayClient(isvAlipayProperties.getServerUrl(),isvAlipayProperties.getAppId(),
        		isvAlipayProperties.getPrivateKey(),isvAlipayProperties.getFormat(),isvAlipayProperties.getCharset(),isvAlipayProperties.getAlipayPublicKey(),isvAlipayProperties.getSignType());
 		AlipayMarketingCardActivateurlApplyRequest  setRequest = new AlipayMarketingCardActivateurlApplyRequest();
        setRequest.putOtherTextParam(AlipayXcxConfig.APP_AUTH_TOKEN_STRING, request.getAppAuthToken());
		AlipayMarketingCardActivateurlApplyModel model = new AlipayMarketingCardActivateurlApplyModel();
		model.setTemplateId(request.getTemplateId());
		model.setCallback(isvAlipayProperties.getMcardNotifyUrl()); //回调地址（必填）
		model.setFollowAppId(request.getFollowAppId());
		model.setOutString(request.getOutString());
		setRequest.setBizModel(model);
		try {
			AlipayMarketingCardActivateurlApplyResponse setResponse  = alipayClient.execute(setRequest);
			log.debug(JSON.toJSONString(setResponse));
			if (setResponse.isSuccess()) {
				url =setResponse.getApplyCardUrl();
				log.debug("调用成功");
			} else {
				log.debug("调用失败");
			}
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}			
		return url;
	}
	

	@Override
	public void messageTopic(MessageTopicRequest request) {
		AlipayClient alipayClient = new DefaultAlipayClient(isvAlipayProperties.getServerUrl(),
				isvAlipayProperties.getAppId(), isvAlipayProperties.getPrivateKey(), isvAlipayProperties.getFormat(),
				isvAlipayProperties.getCharset(), isvAlipayProperties.getAlipayPublicKey(),
				isvAlipayProperties.getSignType());
		AlipayOpenAppMessageTopicSubscribeRequest setRequest = new AlipayOpenAppMessageTopicSubscribeRequest();
        AlipayOpenAppMessageTopicSubscribeModel model = new AlipayOpenAppMessageTopicSubscribeModel();
        model.setAuthType("app_auth");
        model.setAuthToken(request.getAuthToken());
        model.setCommType("HTTP");
        model.setTopic(request.getTopic());
        setRequest.setBizModel(model);
		try {
			AlipayOpenAppMessageTopicSubscribeResponse setResponse = alipayClient.execute(setRequest);
			log.debug(JSON.toJSONString(setResponse));
			if (setResponse.isSuccess()) {
				log.debug("调用成功");
			} else {
				log.debug("调用失败");
			}
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
	}


}
